# coding:utf-8
from django.shortcuts import render
from models import Conser, Common, Feedback
from django.http import HttpResponseRedirect, HttpResponse
from datetime import datetime



# Create your views here.

def login(request):
    """
    普通用户数据
   
    """
    if request.method == 'POST':
        name = request.POST['username']
        psw = request.POST['password']
        user = Common.objects.filter(username=name, password=psw)
        if user:
            request.session['users_id'] = user.first().id
            request.session.set_expiry(60 * 5)
            responss = HttpResponseRedirect('/form/index/')
            return responss
        else:

            return render(request,'entry.html',{'error':'帐号或密码错误'})
    else:
        return render(request, template_name='entry.html')


def sup(request):
    """
    管理员的数据
  
    """
    if request.method == 'POST':
        name = request.POST['username']
        psw = request.POST['password']
        user = Conser.objects.filter(sup_name=name, sup_pass=psw)
        if user:
            request.session['users_id'] = user.first().id
            request.session.set_expiry(60 * 5)
            responss = HttpResponseRedirect('/form/reply/')
            print 'nihao'
            return responss
        else:
            return render(request, 'entry.html', {'error': '帐号或密码错误'})

    else:
        return render(request, template_name='entry.html')


def reply(request):
    """
    管理员登录后到回复界面
   
    """
    users_id = request.session.get('users_id')
    if users_id:
        user = Conser.objects.get(id=users_id)
        res = Feedback.objects.all()
    else:
        return HttpResponseRedirect('/form/login/')
    return render(request, 'reply.html', {'res': res, 'user': user})




def index(request):
    """
    普通用户登录后跳转的页面
    
    """
    users_id = request.session.get('users_id')

    if users_id:
        user = Common.objects.get(id=users_id)
        res = Feedback.objects.all()
        username = user
    else:
        return HttpResponseRedirect('/form/login/')
    return render(request, 'index.html', {'username': username, 'res': res, })


def logout(request):
    """
    普通用户退出
  
    """

    return HttpResponseRedirect('/form/login/')


def regis(request):
    if request.method == 'POST':
        name = request.POST['username']
        psw = request.POST['password']
        u = Common(username=name, password=psw)
        u.save()
        return HttpResponseRedirect('/form/login/')

    else:
        return render(request, template_name='register.html')


def addshuju(request):
    """
    普通用户添加数据
     
    """
    if request.session.get('c_id'):

        return render(request, 'index.html',{'error':'一分钟内不可反馈多次'})
    else:
        users_id = request.session.get('users_id')
        root = Common.objects.get(id=users_id)
        if request.method == 'POST':
            # id = request.POST['id']
            subitime = datetime.now()
            countrnt = request.POST['countent']
            a = Feedback(content=countrnt, user_id=root,submit_time=subitime)
            a.save()
            request.session['c_id'] = True
            request.session.set_expiry(60*5)
            return HttpResponseRedirect('/form/index/')





def fees(request):
        """
        管理员回复信息添加

        """
        users_id = request.session.get('users_id')
        if request.method =='POST':

            ids = request.POST['hah']
            i= ids[0]
            print i
            play = request.POST['playse']
            supname = request.POST['supname']
            reply= datetime.now()
            user = Feedback.objects.get(id=i)
            user.reply=play
            user.replyer = supname
            user.reply_time = reply
            user.save()
            return HttpResponseRedirect('/form/reply/')
        else:
            return render(request, template_name='reply.html')


